package com.jhz.speedy.common.util;

import android.content.Context;
import android.util.DisplayMetrics;
import android.util.TypedValue;

import com.jhz.speedy.component.application.MyApplication;

/**
 * 屏幕尺寸转换
 * Created by JunHeZ on 2017/7/6.
 */
public class ScreenSizeUtils {

    public static int dp2px(Context context, int dp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, context.getResources().getDisplayMetrics());
    }

    public static int dp2px(int dp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
                MyApplication.getInstance().getResources().getDisplayMetrics());
    }

    public static int dp2px(float dp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
                MyApplication.getInstance().getResources().getDisplayMetrics());
    }

    public static int px2dp(int px) {
        float scale = MyApplication.getInstance().getResources().getDisplayMetrics().density;
        return (int) (px / scale + 0.5f);
    }

    public static int px2dp(float px) {
        float scale = MyApplication.getInstance().getResources().getDisplayMetrics().density;
        return (int) (px / scale + 0.5f);
    }


    public static int getScreenWidth() {
        DisplayMetrics metrics = MyApplication.getInstance().getResources().getDisplayMetrics();
        return metrics.widthPixels;
    }


    public static int getScreenHeight() {
        DisplayMetrics metrics = MyApplication.getInstance().getResources().getDisplayMetrics();
        return metrics.heightPixels;
    }
}
